package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.constant.Url;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.BR;
import com.qire.manhua.activity.BuyMembershipDialogActivity;
import com.qire.manhua.databinding.ActivityBuyMembershipDialogBinding;
import com.qire.manhua.dialog.PayWaySelectorOfBuyVipDialog;
import com.qire.manhua.dialog.TeensPasswordAuthorizeDialog;
import com.qire.manhua.model.enumeration.PaymentMethod;
import com.qire.manhua.model.enumeration.TeensModeScene;
import com.qire.manhua.model.viewModel.impl.BuyMembershipDialogViewModel;
import com.qire.manhua.model.viewModel.impl.BuyMembershipDialogViewModel.MembersPackageItemViewModel;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class BuyMembershipDialogController extends EventController<BuyMembershipDialogActivity> implements AutoVariable {

    private BuyMembershipDialogViewModel viewModel;

    public BuyMembershipDialogController(@NonNull BuyMembershipDialogActivity owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        ActivityBuyMembershipDialogBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setBuyMembershipDialogController(this);
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.buyMembershipDialogController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(BuyMembershipDialogViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void closeBtnOnClick(View view) {
        owner.onBackPressed();
    }

    public void payButtonOnClick(View view) {
        if(TeensModeManage.teensModeEnable()) {
            new TeensPasswordAuthorizeDialog(view.getContext(), TeensModeScene.Recharge, this::onPay).show();
            return;
        }
        onPay();
    }

    private void onPay() {
        if (!viewModel.haveChosen()) {
            ToastHelper.showSnackBar("请先选择购买套餐", Prompt.WARNING);
            return;
        }

        new PayWaySelectorOfBuyVipDialog(owner, v -> {
            PaymentMethod paymentMethod = ObjectUtil.as(v.getTag(), PaymentMethod.class);
            if(ObjectUtil.nonNull(paymentMethod)) {
                viewModel.buyBuyMembership(paymentMethod, owner.getClass().getSimpleName());
            }
        }).show();
    }

    public void waresLayoutOnClick(View view) {
        MembersPackageItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), MembersPackageItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            itemViewModel.toggle();
        }
    }

    public void protocolBtnOnClick(View view) {
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.WebPage)
                .withData("title", "付费会员服务协议")
                .withData("url", Url.buyMemberProtocol_url)
                .navigation();
    }

}
